﻿namespace Numbrella.Client
{
	using System;
	using System.ComponentModel;
	using System.Windows;

	internal class CurrentProblemDataProvider : INotifyPropertyChanged
	{
		private readonly TaskSolvingEngine taskSolvingEngine;

		public CurrentProblemDataProvider()
		{
			// To support visual designers, which use their own application objects
			if (Application.Current is App)
			{
				this.taskSolvingEngine = ((App) Application.Current).SolvingEngine;
				this.taskSolvingEngine.ProblemDefinitionChanged += this.OnTaskSolvingEngineProblemDefinitionChanged;
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public string ProblemName
		{
			get
			{
				return this.taskSolvingEngine == null || this.taskSolvingEngine.CurrentProblemDefinition == null
				       	? String.Empty
				       	: this.taskSolvingEngine.CurrentProblemDefinition.Name;
			}
		}

		public string ProblemDescriptionText
		{
			get
			{
				return this.taskSolvingEngine == null || this.taskSolvingEngine.CurrentProblemDefinition == null
				       	? String.Empty
				       	: this.taskSolvingEngine.CurrentProblemDefinition.Description.Text;
			}
		}

		private void OnTaskSolvingEngineProblemDefinitionChanged(object sender, EventArgs e)
		{
			this.RaisePropertyChanged(new PropertyChangedEventArgs("ProblemName"));
			this.RaisePropertyChanged(new PropertyChangedEventArgs("ProblemDescriptionText"));
		}

		private void RaisePropertyChanged(PropertyChangedEventArgs e)
		{
			PropertyChangedEventHandler handler = this.PropertyChanged;
			if (handler != null)
				handler(this, e);
		}
	}
}
